﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Navigation;

namespace FasterWPF
{
    public static class ContextMenuExt
    {
        public static void ApplyContextMenuStrategy(this ContextMenu cme, string backgroundColor, string borderColor, Thickness borderThickness, string dropShadowColor, string menuItemBackgroundHighlightColor, string menuItemInnerHighlightBorder, string menuItemOuterHighlightBorder, MenuItem mie1 = null, MenuItem mie2 = null, MenuItem mie3 = null, MenuItem mie4 = null, MenuItem mie5 = null, MenuItem mie6 = null, MenuItem mie7 = null, MenuItem mie8 = null, MenuItem mie9 = null, MenuItem mie10 = null)
        {
            cme.ApplyContextMenuStrategy(backgroundColor, borderColor, borderThickness, dropShadowColor);

            if (mie1 != null)
            {
                mie1.ApplyContextMenuItemStrategy(menuItemBackgroundHighlightColor, menuItemInnerHighlightBorder, menuItemOuterHighlightBorder);
            }

            if (mie2 != null)
            {
                mie2.ApplyContextMenuItemStrategy(menuItemBackgroundHighlightColor, menuItemInnerHighlightBorder, menuItemOuterHighlightBorder);
            }

            if (mie3 != null)
            {
                mie3.ApplyContextMenuItemStrategy(menuItemBackgroundHighlightColor, menuItemInnerHighlightBorder, menuItemOuterHighlightBorder);
            }

            if (mie4 != null)
            {
                mie4.ApplyContextMenuItemStrategy(menuItemBackgroundHighlightColor, menuItemInnerHighlightBorder, menuItemOuterHighlightBorder);
            }

            if (mie5 != null)
            {
                mie5.ApplyContextMenuItemStrategy(menuItemBackgroundHighlightColor, menuItemInnerHighlightBorder, menuItemOuterHighlightBorder);
            }

            if (mie6 != null)
            {
                mie6.ApplyContextMenuItemStrategy(menuItemBackgroundHighlightColor, menuItemInnerHighlightBorder, menuItemOuterHighlightBorder);
            }

            if (mie7 != null)
            {
                mie7.ApplyContextMenuItemStrategy(menuItemBackgroundHighlightColor, menuItemInnerHighlightBorder, menuItemOuterHighlightBorder);
            }

            if (mie8 != null)
            {
                mie8.ApplyContextMenuItemStrategy(menuItemBackgroundHighlightColor, menuItemInnerHighlightBorder, menuItemOuterHighlightBorder);
            }

            if (mie9 != null)
            {
                mie9.ApplyContextMenuItemStrategy(menuItemBackgroundHighlightColor, menuItemInnerHighlightBorder, menuItemOuterHighlightBorder);
            }

            if (mie10 != null)
            {
                mie10.ApplyContextMenuItemStrategy(menuItemBackgroundHighlightColor, menuItemInnerHighlightBorder, menuItemOuterHighlightBorder);
            }
        }

        public static void ApplyContextMenuStrategy(this ContextMenu cme, string backgroundColor, string borderColor, Thickness borderThickness, string dropShadowColor)
        {
            string xaml = "<ControlTemplate TargetType=\"{x:Type ContextMenu}\"><theme:SystemDropShadowChrome Name=\"Shdw\" Color=\"Transparent\"   SnapsToDevicePixels=\"true\"><Border Name=\"ContextMenuBorder\" Background=\"" + backgroundColor + "\" BorderBrush=\"" + borderColor + "\" BorderThickness=\"" + borderThickness + "\"><Grid><Rectangle Fill=\"#F1F1F1\" HorizontalAlignment=\"Left\" Width=\"28\" Margin=\"2\" RadiusX=\"2\" RadiusY=\"2\"/><Rectangle HorizontalAlignment=\"Left\" Width=\"1\" Margin=\"30,2,0,2\" Fill=\"#E2E3E3\"/><Rectangle HorizontalAlignment=\"Left\" Width=\"1\" Margin=\"31,2,0,2\" Fill=\"White\"/><ScrollViewer Name=\"ContextMenuScrollViewer\" CanContentScroll=\"false\" Grid.ColumnSpan=\"2\" Margin=\"1,0\" Style=\"{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type FrameworkElement}, ResourceId=MenuScrollViewer}}\"><Grid RenderOptions.ClearTypeHint=\"Enabled\"><Canvas Height=\"0\" Width=\"0\" HorizontalAlignment=\"Left\" VerticalAlignment=\"Top\"><Rectangle Height=\"{Binding ElementName=ContextMenuBorder,Path=ActualHeight}\" Width=\"{Binding ElementName=ContextMenuBorder,Path=ActualWidth}\" Fill=\"{Binding ElementName=ContextMenuBorder,Path=Background}\" /></Canvas><ItemsPresenter Name=\"ItemsPresenter\" Margin=\"{TemplateBinding Padding}\" KeyboardNavigation.DirectionalNavigation=\"Cycle\" SnapsToDevicePixels=\"{TemplateBinding SnapsToDevicePixels}\"/></Grid></ScrollViewer></Grid></Border></theme:SystemDropShadowChrome><ControlTemplate.Triggers><Trigger Property=\"HasDropShadow\" Value=\"true\"><Setter TargetName=\"Shdw\" Property=\"Margin\" Value=\"0,0,5,5\"/><Setter TargetName=\"Shdw\" Property=\"Color\" Value=\"" + dropShadowColor + "\"/></Trigger></ControlTemplate.Triggers></ControlTemplate>";

            MemoryStream sr = new MemoryStream(Encoding.ASCII.GetBytes(xaml));
            ParserContext pc = new ParserContext();
            pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
            pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
            pc.XmlnsDictionary.Add("theme", "clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero");
            var repBtnTemplate = (ControlTemplate)XamlReader.Load(sr, pc);
            cme.Template = repBtnTemplate;
        }

        public static void ApplyContextMenuItemStrategy(this MenuItem mie, string backgroundHighlightColor, string innerHighlightBorder, string outerHighlightBorder)
        {
            string xaml = "<ControlTemplate x:Key=\"{ComponentResourceKey TypeInTargetAssembly={x:Type MenuItem}, ResourceId=TopLevelItemTemplateKey}\" TargetType=\"{x:Type MenuItem}\"><ControlTemplate.Resources><Geometry x:Key=\"Checkmark\">M 0,5.1 L 1.7,5.2 L 3.4,7.1 L 8,0.4 L 9.2,0 L 3.3,10.8 Z</Geometry><LinearGradientBrush x:Key=\"MenuItemPressedFill\" StartPoint=\"0,0\" EndPoint=\"0,1\"><LinearGradientBrush.GradientStops><GradientStop Color=\"#28717070\" Offset=\"0\"/><GradientStop Color=\"#50717070\" Offset=\"0.75\"/><GradientStop Color=\"#90717070\" Offset=\"1\"/></LinearGradientBrush.GradientStops></LinearGradientBrush></ControlTemplate.Resources><Grid SnapsToDevicePixels=\"true\"><Rectangle x:Name=\"OuterBorder\" RadiusX=\"2\" RadiusY=\"2\"/><Rectangle Name=\"Bg\" Margin=\"1\" Fill=\"{TemplateBinding MenuItem.Background}\" Stroke=\"{TemplateBinding MenuItem.BorderBrush}\" StrokeThickness=\"1\" RadiusX=\"1\" RadiusY=\"1\"/><Rectangle x:Name=\"InnerBorder\" Margin=\"2\"/><DockPanel><ContentPresenter x:Name=\"Icon\" Margin=\"4,0,6,0\" VerticalAlignment=\"Center\" ContentSource=\"Icon\" SnapsToDevicePixels=\"{TemplateBinding SnapsToDevicePixels}\"/><Path x:Name=\"GlyphPanel\" Margin=\"7,0,0,0\" Visibility=\"Collapsed\" VerticalAlignment=\"Center\" Fill=\"{TemplateBinding MenuItem.Foreground}\" FlowDirection=\"LeftToRight\" Data=\"{StaticResource Checkmark}\"/><ContentPresenter ContentSource=\"Header\" Margin=\"{TemplateBinding MenuItem.Padding}\" RecognizesAccessKey=\"True\" SnapsToDevicePixels=\"{TemplateBinding SnapsToDevicePixels}\"/></DockPanel></Grid><ControlTemplate.Triggers><Trigger Property=\"Icon\" Value=\"{x:Null}\"><Setter TargetName=\"Icon\" Property=\"Visibility\" Value=\"Collapsed\"/></Trigger><Trigger Property=\"IsChecked\" Value=\"true\"><Setter TargetName=\"GlyphPanel\" Property=\"Visibility\" Value=\"Visible\"/><Setter TargetName=\"Icon\" Property=\"Visibility\" Value=\"Collapsed\"/></Trigger><Trigger Property=\"IsHighlighted\" Value=\"true\"><Setter TargetName=\"Bg\" Property=\"Stroke\" Value=\"#90717070\"/><Setter TargetName=\"OuterBorder\" Property=\"Stroke\" Value=\"" + outerHighlightBorder + "\"/><Setter TargetName=\"InnerBorder\" Property=\"Stroke\" Value=\"#50FFFFFF\"/></Trigger><Trigger Property=\"IsKeyboardFocused\" Value=\"true\"><Setter TargetName=\"Bg\" Property=\"Stroke\" Value=\"#E0717070\"/><Setter TargetName=\"Bg\" Property=\"Fill\" Value=\"" + backgroundHighlightColor + "\"/><Setter TargetName=\"InnerBorder\" Property=\"Stroke\" Value=\"" + innerHighlightBorder + "\"/></Trigger><Trigger Property=\"IsEnabled\" Value=\"false\"><Setter Property=\"Foreground\" Value=\"#FF9A9A9A\"/><Setter TargetName=\"GlyphPanel\" Property=\"Fill\" Value=\"#848589\"/></Trigger></ControlTemplate.Triggers></ControlTemplate>";

            MemoryStream sr = new MemoryStream(Encoding.ASCII.GetBytes(xaml));
            ParserContext pc = new ParserContext();
            pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
            pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
            pc.XmlnsDictionary.Add("theme", "clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero");
            var repBtnTemplate = (ControlTemplate)XamlReader.Load(sr, pc);
            mie.Template = repBtnTemplate;
        }
                
    }//end of class
}//end of namespace
